package com.example.demo.javaTest;

import com.example.demo.specialException.AgeIllegalRuntimeException;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.jupiter.api.*;

import java.math.BigDecimal;
import java.util.*;
import java.util.function.IntToDoubleFunction;

public class Syntax {

    //构造器(可重载)
    public Syntax() {

    }

    @BeforeEach
    public void before(){
        System.out.println("执行BeforeEach");
    }

    @AfterEach
    public void after(){
        System.out.println("执行AfterEach");
    }

    @BeforeAll
    public static void beforeClass(){
        System.out.println("执行BeforeAll");
    }

    @AfterAll
    public static void afterClass(){
        System.out.println("执行AfterAll");
    }

    @Test
    public void testSyntax() {
        int a1 = 0B01100001;//二进制
        int a2 = 0141;//八进制
        int a3 = 0XFA;//十六进制
        System.out.println(a1);
        Assertions.assertEquals(97, a1, "a1");
        System.out.println(a2);
        Assertions.assertEquals(97, a2, "a2");
        System.out.println(a3);
        Assertions.assertEquals(250, a3, "a3");
        byte i = 10;
        short j = 30;
        int k = i + j;
        System.out.println(k);
        Random r = new Random();
        int number = r.nextInt(10);//0-9
        System.out.println(number);
        int[] ages = new int[]{12, 24, 36};
        int[] height = {12, 24, 36};
        System.out.println(ages[0]);
        System.out.println(height[1]);
        int[] sysDefault = new int[3];
        for (int index = 0; index < sysDefault.length; index++) {
            System.out.println(sysDefault[index]);
        }
        for (int value : sysDefault) {
            System.out.println(value);
        }
        int c = add(1, 2);
        System.out.println(c);
    }

    @Test
    public void testUtils() {
        ArrayList<String> a = new ArrayList<>();
        a.add("1");
        a.add("2");
        ArrayList<String> b = new ArrayList<>();
        b.add("1");
        b.add("2");
        System.out.println(a.equals(b));

        Runtime runtime = Runtime.getRuntime();
        System.out.println(runtime.totalMemory());
        System.out.println(runtime.freeMemory());
        System.out.println(runtime.availableProcessors());
    }

    @Test
    public void testString() {
        StringBuilder s = new StringBuilder("a");
        s.append("b").append(12).append(true).reverse();
        System.out.println(s);
        System.out.println(s.length());

        String rs = s.toString();
        System.out.println(rs);

        StringJoiner sj = new StringJoiner(",", "[", "]");
        sj.add("a").add("b").add("c");
        System.out.println(sj);//[a,b,c]
    }

    @Test
    public void testNumber() {

        double a = 0.1;
        double b = 0.2;
        double c = a + b;
        System.out.println(c);//0.300000004
        BigDecimal a1 = BigDecimal.valueOf(a);
        BigDecimal b1 = BigDecimal.valueOf(b);
        BigDecimal c1 = a1.add(b1);//substract multiply divide
        System.out.println(c1);//0.3
        double rs = c1.doubleValue();
        System.out.println(rs);//0.3
    }

    @Test
    public void testArray() {
        int[] arr = {10, 20, 30, 40, 50, 60};
        System.out.println(Arrays.toString(arr));
        int[] arr2 = Arrays.copyOfRange(arr, 1, 4);
        System.out.println(Arrays.toString(arr2));
        int[] arr3 = Arrays.copyOf(arr, 10);
        System.out.println(Arrays.toString(arr3));

        double[] price = {99.8, 128, 100};
        Arrays.setAll(price, new IntToDoubleFunction() {
            @Override
            public double applyAsDouble(int value) {
                return price[value] * 0.8;
            }
        });
        System.out.println(Arrays.toString(price));
        Arrays.sort(price);
        Arrays.setAll(price, value -> price[value] * 0.8);
        System.out.println(Arrays.toString(price));

        String[] names = {"aC", "cB", "Ab"};
        Arrays.sort(names, String::compareToIgnoreCase);
        System.out.println(Arrays.toString(names));

    }

    @Test
    public void testRegexp() {
        String QQ = "234hj35h";
        boolean matches = QQ.matches("[1-9]\\d{5,19}");
        System.out.println(matches);
    }

    @Test
    public void testException() {
        try {
            throw new AgeIllegalRuntimeException("Age is illegal");
        } catch (AgeIllegalRuntimeException e) {
            System.out.println(e.toString());
        } finally {

        }
    }

    @Test
    public void testParams() {
        System.out.println(add2());
        System.out.println(add2(1));
        System.out.println(add2(1, 2, 3));
        System.out.println(add2(new int[]{1, 2, 3}));
    }

    public static int add2(int... nums) {
        int a = 0;
        for (int s : nums) {
            a += s;
        }
        return a;
    }

    public static int add(int a, int b) {
        return a + b;
    }

}



